import random
from datetime import timedelta, datetime

import pandas as pd

from util.common import *

if __name__ == '__main__':

    list = []

    days = generate_day('2024-01-05', '2024-05-02', 7) + generate_day('2024-08-09', '2024-11-22', 7)
    # days =  generate_day('2024-07-12', '2024-11-22', 7)

    df = read_excel("source/tf_smp_web_asset2.xlsx")
    filtered_df = df[df['PUBLIC_WEB_IP'].notna()]

    contents = read_sql_file('source/tf_smp_port_exe_result.sql')

    for createTime in days:
        start_date = end_date = datetime.strptime(createTime, '%Y-%m-%d %H:%M:%S')
        timeSpan = random.randrange(2, 100)
        id = "PORTZX" + random_string(14)

        for row in df.itertuples():
            assetId = row.ID
            ip = None if pd.isna(row.INTRANET_IP) else row.INTRANET_IP
            port = None if pd.isna(row.INTRANET_PORT) else int(row.INTRANET_PORT)

            item = random.choice(contents)

            secondSpan = random.randrange(220, 243)
            end_date += timedelta(seconds=secondSpan)
            seep = {
                "id": "WEREZX" + random_string(14),
                "exeId": id,
                "assetId": assetId,
                "ip": ip,
                "ipval": None,
                "port": port,
                "startTime": start_date,
            }
            list.append(render_template(item, seep))

        seep = {
            "id": id,
            "taskId": "TASK6865227906808243",
            "startTime": start_date,
            "endTime": end_date,
            "exeTime": int((end_date - start_date).total_seconds() / 60),
        }
        message1 = render_template("""
                  insert into tf_smp_port_task_exe (EXE_ID, TASK_ID, EXE_TIME, EXE_STATUS, EXE_PERCENT, EXE_DESP, CREATE_TIME, UPDATE_TIME, TARGET_NUM)
                  values ($id, $taskId, $exeTime, '2', 100, '执行成功', $startTime, $endTime, null);
               """, seep)
        list.append(message1)

    write_file('target/SATP_端口扫描补数据.sql', list)
       
